package com.zysdev.openglbeautydemo

import android.Manifest
import android.R.attr
import android.content.pm.PackageManager
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.CheckBox
import android.widget.RadioGroup
import com.zysdev.openglbeautydemo.filter.CameraView
import com.zysdev.openglbeautydemo.widget.RecordButton
import android.R.attr.path

import android.graphics.BitmapFactory

import android.graphics.Bitmap


class MainActivity : AppCompatActivity(), RecordButton.OnRecordListener,
    RadioGroup.OnCheckedChangeListener {

    private var cameraView: CameraView? = null
    private var lastShowGiftTime = 0;
    private var bitmap:Bitmap?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        cameraView = findViewById(R.id.cameraView)
        val btnRecord: RecordButton = findViewById(R.id.btn_record)
        btnRecord.setOnRecordListener(this)

        //速度
        val rgSpeed = findViewById<RadioGroup>(R.id.rg_speed)
        rgSpeed.setOnCheckedChangeListener(this)

        (findViewById<View>(R.id.beauty) as CheckBox).setOnCheckedChangeListener { _, isChecked ->
            cameraView?.enableBeauty(isChecked)
        }
        findViewById<Button>(R.id.btn_gift).setOnClickListener {
            if (System.currentTimeMillis() - lastShowGiftTime > 5000) {
                bitmap = BitmapFactory.decodeResource(this.resources, R.drawable.ic_hj)
                if (bitmap!=null){
                    cameraView?.showGift(bitmap!!)
                }
            }else{
                if (bitmap!=null){
                    bitmap!!.recycle()
                    bitmap=null
                }
                cameraView?.goneGift()
            }
        }
        checkPermission()
    }


    private fun checkPermission(): Boolean {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            requestPermissions(
                arrayOf(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA
                ), 1
            )
        }
        return false
    }


    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        when (checkedId) {
            R.id.btn_extra_slow -> cameraView?.setSpeed(CameraView.Speed.MODE_EXTRA_SLOW)
            R.id.btn_slow -> cameraView?.setSpeed(CameraView.Speed.MODE_SLOW)
            R.id.btn_normal -> cameraView?.setSpeed(CameraView.Speed.MODE_NORMAL)
            R.id.btn_fast -> cameraView?.setSpeed(CameraView.Speed.MODE_FAST)
            R.id.btn_extra_fast -> cameraView?.setSpeed(CameraView.Speed.MODE_EXTRA_FAST)
        }
    }

    override fun onRecordStart() {
        cameraView?.startRecord()
    }

    override fun onRecordStop() {
        Log.i("tuch", "onRecordStop: ----------------->")
        cameraView?.stopRecord()
    }

}